package cn.iocoder.yudao.module.fm.controller.admin.fileinfo;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfourl.vo.FileInfoUrlRespVO;
import cn.iocoder.yudao.module.fm.convert.fileinfourl.FileInfoUrlConvert;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfourl.FileInfoUrlDO;
import cn.iocoder.yudao.module.fm.dal.mysql.fileinfourl.FileInfoUrlMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfo.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfo.FileInfoDO;
import cn.iocoder.yudao.module.fm.convert.fileinfo.FileInfoConvert;
import cn.iocoder.yudao.module.fm.service.fileinfo.FileInfoService;

@Tag(name = "管理后台 - 文件信息")
@RestController
@RequestMapping("/fm/file-info")
@Validated
public class FileInfoController {

    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private FileInfoUrlMapper fileInfoUrlMapper;


    @PostMapping("/create")
    @Operation(summary = "创建文件信息")
    @PreAuthorize("@ss.hasPermission('fm:file-info:create')")
    public CommonResult<Long> createFileInfo(@Valid @RequestBody FileInfoCreateReqVO createReqVO) {
        return success(fileInfoService.createFileInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件信息")
    @PreAuthorize("@ss.hasPermission('fm:file-info:update')")
    public CommonResult<Boolean> updateFileInfo(@Valid @RequestBody FileInfoUpdateReqVO updateReqVO) {
        fileInfoService.updateFileInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fm:file-info:delete')")
    public CommonResult<Boolean> deleteFileInfo(@RequestParam("id") Long id) {
        fileInfoService.deleteFileInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fm:file-info:query')")
    public CommonResult<FileInfoRespVO> getFileInfo(@RequestParam("id") Long id) {
        FileInfoDO fileInfo = fileInfoService.getFileInfo(id);
        FileInfoRespVO fileInfoRespVO = FileInfoConvert.INSTANCE.convert(fileInfo);

        List<FileInfoUrlRespVO> fileInfoUrlRespVOS = FileInfoUrlConvert.INSTANCE.convertList(fileInfoUrlMapper.selectList(Wrappers.<FileInfoUrlDO>lambdaQuery().eq(FileInfoUrlDO::getParentId, fileInfoRespVO.getId())));
        fileInfoRespVO.setFileUrlList(fileInfoUrlRespVOS);

        return success(fileInfoRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得文件信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fm:file-info:query')")
    public CommonResult<List<FileInfoRespVO>> getFileInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<FileInfoDO> list = fileInfoService.getFileInfoList(ids);
        return success(FileInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件信息分页")
    @PreAuthorize("@ss.hasPermission('fm:file-info:query')")
    public CommonResult<PageResult<FileInfoRespVO>> getFileInfoPage(@Valid FileInfoPageReqVO pageVO) {
        PageResult<FileInfoDO> pageResult = fileInfoService.getFileInfoPage(pageVO);
        PageResult<FileInfoRespVO> fileInfoRespVOPageResult = FileInfoConvert.INSTANCE.convertPage(pageResult);
        List<FileInfoRespVO> list = fileInfoRespVOPageResult.getList();
        for (FileInfoRespVO fileInfoRespVO : list) {
            List<FileInfoUrlRespVO> fileInfoUrlRespVOS = FileInfoUrlConvert.INSTANCE.convertList(fileInfoUrlMapper.selectList(Wrappers.<FileInfoUrlDO>lambdaQuery().eq(FileInfoUrlDO::getParentId, fileInfoRespVO.getId())));
            fileInfoRespVO.setFileUrlList(fileInfoUrlRespVOS);
        }

        return success(fileInfoRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件信息 Excel")
    @PreAuthorize("@ss.hasPermission('fm:file-info:export')")
    @OperateLog(type = EXPORT)
    public void exportFileInfoExcel(@Valid FileInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FileInfoDO> list = fileInfoService.getFileInfoList(exportReqVO);
        // 导出 Excel
        List<FileInfoExcelVO> datas = FileInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "文件信息.xls", "数据", FileInfoExcelVO.class, datas);
    }

}
